package kr.example.kaist_yasik;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import kr.example.kaist_yasik.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends Activity implements M{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
		
		if(M.m.picket == -1){
			AssetManager am = getResources().getAssets();
			InputStream is = null;
			try {
				is = am.open("database.csv");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				//System.exit(0);
			}
			M.m.Load(is);
			M.m.printLog();
			M.m.picket = 0;
		}
		
		
		final String[] menu_show = m.menuShow();
		ArrayAdapter<String> adapt =
				new ArrayAdapter<String>(this, R.layout.menu_item, menu_show);
		menuList.setAdapter(adapt);
		
		menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
                // Note: if the list was built "by hand" the id could be used.
                // As-is, though, each item has the same id
                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();
                
                for(int j = 0; j < menu_show.length; j++){
                	if (strText.equalsIgnoreCase(menu_show[j])) {
                		m.picket=j;
                        // Launch the Game Activity
                        startActivity(new Intent(MainActivity.this, SubActivity.class));
                    }
                }
            }
        });
	}

	
	/*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}*/
	/*
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}*/


}